const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({
  data: {
    portrait:'http://xianzhi.leaf-tech.net:8081/static/mini/mrtx.png',
    nick:'',
    mobile:'',
    wechat:'',
    idcardNumber:'',
    infodata:''
  },
  onLoad: function (options) {
    // this.info()
    if(options.parentid){
      app.globalData.parentid=options.parentid
    }
  },
  info(){
    Controller.info().then(res=>{
      if(res.data.baseData){
        this.setData({
          infodata:res.data.baseData || '',
          nick:res.data.baseData.nick || '',
          portrait:res.data.baseData.portrait || '',
          mobile:res.data.baseData.mobile || '',
          wechat:res.data.baseData.wechat || '',
          idcardNumber:res.data.baseData.idcardNumber || ''
        })
      }
    })
  },
  changeinput(e){
    var name=e.currentTarget.dataset.name;
    this.setData({
      [name]:e.detail.value
    })
  },
  upShopLogo: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#222",
      success: function (res) {
        if(!res.cancel) {
          if(res.tapIndex == 0) {
            that.chooseWxImageShop('album');//从相册中选择
          }else if(res.tapIndex == 1) {
            that.chooseWxImageShop('camera');//手机拍照
          }
        }
      }
    })
  },
  chooseWxImageShop: function (type) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
        success: function (res) {
        // that.setData({
        //   portrait: res.tempFilePaths[0]
        // })
        wx.navigateTo({
          url: '/pages/Me/cropper?img='+res.tempFilePaths[0],
        })
      }
    })
  },
  comfirm(e){
    if (this.data.mobile && !/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(this.data.mobile)) {
        wx.showToast({
          title: "联系电话格式错误",
          icon: 'none',
          duration: 1000
        });
        return
    }
    var data={
      nick:this.data.nick,
      portrait:this.data.portrait,
      mobile:this.data.mobile,
      wechat:this.data.wechat,
      idcardNumber:this.data.idcardNumber
    }
    Controller.updateInfo(data).then(res=>{
      wx.showToast({
        title: "修改成功",
        icon: 'none',
        duration: 1000
      });
      setTimeout(function(){
        wx.navigateBack({
          delta: 1
        })
      },1000)
    })
    
  },
  onShareAppMessage: function (e) {
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
  onShareTimeline: function(res){
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
})